<?php

namespace App\Model;

use Illuminate\Http\Request;

class Wish extends BaseModel
{
    protected $connection = 'api';
    protected $table = 'wish';
    
    public function getList(Request $request){
        $limit = $request->input('limit',10);
        $page = $request->input('page',1);
        $uuid = $request->input('uuid');
        $nickname = $request->input('nickname');
        $begin_date = $request->input('begin_date');
        $end_date = $request->input('end_date');
        $query = new self();
        
        // todo 搜索
        if($begin_date && $end_date) $query = $query->whereBetween('created_at',[$begin_date, $end_date]);
        if($begin_date && !$end_date) $query = $query->where('created_at', '>=', $begin_date);
        if(!$begin_date && $end_date) $query = $query->where('created_at', '<=', $begin_date);
        
        return $query->where('is_delete',0)->whereHas('number', function($query) use($uuid, $nickname) {
            if($uuid) $query->where('uuid','like',$uuid."%");
            if($nickname) $query->where('nickname','like',"%{$nickname}%");
        })->with(['number:user_id,nickname,uuid','gift:gift_id,name'])
            ->paginate($limit,['*'],'page',$page);
    }
    
    public function deleteWish($id) {
        return curl_request(getKkaudioApiUrl().'/inner/wish/del',['wish_id' => $id ], 'urlencoded', 'GET');
    }
    
    public function number() {
        return $this->belongsTo(Number::class,'user_id','user_id')->withDefault([
            'nickname' => '未知',
            'uuid' => '未知'
        ]);
    }
    
    public function gift() {
        return $this->belongsTo(GiftLibrary::class,'gift_id','gift_id')->withDefault([
            'name' => '未知'
        ]);
    }
}
